import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class addAgent extends HttpServlet
{
	public static dbapi conn;
	// Add new agent to the database
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		// Create dbapi instance
		conn = new dbapi();
		// Connect to Oracle
		conn.connect();
		out.println(htmlHeader.header);
		
		// If servlet cannot connect to the db throw error
		if(!conn.connected)
		{
			out.println("CANT CONNECT! <br>");
			out.println(conn.msg + "<br>");
			return;
		}


		String agentName = request.getParameter("AGENTNAME");
		boolean worked = conn.addAgent(agentName);
		
		// If the transaction works display message and refresh to agent list 
		// If not print an error message
		if (!worked)
		{
			out.println("Error adding agent to database. <br>"+conn.msg);
		}else{
			out.println("<header><meta http-equiv=\"refresh\" content=\"2;getAgents\"></header><body>Agent Added Successfully...<br>");
		}
		
		return;
	}
}
